/*
 * @Author: LSC
 */
const Mock = require('mockjs')
function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}
var productGroupList = []
const count = 3
let groups = [
  {
    groupName: 'Customize',
    quantity: [0, 1]
  },
  {
    groupName: 'Selected Design',
    quantity: [2, 3, 4, 5, 6, 7, 8]
  },
  {
    groupName: 'Wearables',
    quantity: [9, 10]
  },
]
for (let i = 0; i < count; i++) {
  productGroupList.push(Mock.mock({
    id: i,
    groupName: groups[i].groupName,
    description: 'Description' + i,
    quantity: groups[i].quantity,
    'sequence|1': [{ value: '0', label: 'By number (Big to small)' }, { value: '1', label: 'By creation time (Reverse order)' }, { value: '2', label: 'By creation time (Positive order)' }],
    status: { value: '1', label: 'Enable' },//[{ value: '0', label: 'Disable' }, { value: '1', label: 'Enable' }]
    products: []
  }))
}
module.exports = [
  // username search
  {
    url: '/vue-element-admin/get/productGroupList',
    type: 'get',
    response: config => {
      const { page = 1, limit = 10, name } = config.query
      let searchArr = JSON.parse(JSON.stringify(productGroupList))
      //条件搜索
      if (name) {
        searchArr = searchArr.filter(item => item.groupName == name)
      }

      const pageList = searchArr.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        count: searchArr.length,
        data: pageList
      }
    }
  },
  {
    url: '/vue-element-admin/getProductGroupById',
    type: 'get',
    response: config => {
      const { id } = config.query
      let searchArr = JSON.parse(JSON.stringify(productGroupList))
      searchArr = searchArr.filter(item => item.id == id)[0]
      return {
        code: 20000,
        data: searchArr
      }
    }
  },
  {
    url: '/vue-element-admin/setGroup',
    type: 'post',
    response: config => {
      const { id, status } = config.body
      //条件搜索
      productGroupList = productGroupList.map(item => {
        if (item.id == id) {
          item.status = status
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  }, {
    url: '/vue-element-admin/setListGroup',
    type: 'post',
    response: config => {
      const { newList } = config.body
      //条件搜索
      productGroupList = newList

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  {
    url: '/vue-element-admin/addGroup',
    type: 'post',
    response: config => {
      const { id, description, groupName, sequence, products, quantity = [] } = config.body
      if (id === undefined) {
        //条件搜索
        productGroupList.push(Mock.mock({
          id: productGroupList.length,
          groupName: groupName,
          description: description,
          quantity,
          sequence,
          status: { value: '0', label: 'Disable' }
        }))
      } else {
        productGroupList = productGroupList.map(item => {
          if (item.id == id) {
            item.description = description
            item.groupName = groupName
            item.sequence = sequence
            item.products = products || []
            item.quantity = quantity
          }
          return item
        })
      }


      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  }

]
